package com.tuyue.police.biz.impl;

import com.tuyue.police.biz.IUserBiz;
import com.tuyue.police.dao.IUserDao;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.User;
import com.tuyue.police.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/13.
 * @Modified By:
 */
@Service
public class UserBizImpl implements IUserBiz {

    @Autowired
    private IUserDao userDao;
    @Override
    public User login(String userName, String userPassword) {
        User user = userDao.findByUserName(userName);
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        if (!user.getUserPassword().equals(MD5.md5(userPassword))) {
            throw new MyException(ResultEnum.USER_PASSWORDERROR);
        }
        user.setUserPassword(null);
        return user;
    }

    @Override
    public User regist(User user) {
        if (StringUtils.isEmpty(user.getUserName())) {
            throw new MyException(ResultEnum.USER_USERNAMENOTNULL);
        }
        if (StringUtils.isEmpty(user.getUserPassword())) {
            throw new MyException(ResultEnum.USER_PASSWORDNOTNULL);
        }
        if (StringUtils.isEmpty(user.getType())) {
            throw new MyException(ResultEnum.USER_TYPENOTNULL);
        }
        if (user.getType()<=0) {
            throw new MyException(ResultEnum.USER_TYPE_ERROR);
        }
        User userDaoByUserName = userDao.findByUserName(user.getUserName());
        if (!ObjectUtils.isEmpty(userDaoByUserName)) {
            throw new MyException(ResultEnum.USER_USERNAMEISEXISTS);
        }
        user.setUserPassword(MD5.md5(user.getUserPassword()));
        user.setUserTag(MD5.md5(user.getUserName()));
        user.setCreatTime(new Timestamp(new Date().getTime()));
        User save = userDao.save(user);
        save.setUserPassword("");
        return save;
    }

    @Override
    public User findById(Long userId) {
        User user = userDao.findOne(userId);
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        return user;
    }

    @Override
    public User findByPhome(String phome) {
        User user = userDao.findByUserName(phome);
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        return user;
    }

    @Override
    public User findByopenId(String openId) {
        User user = userDao.findByUserOpenId(openId);
        return user;

    }

    @Override
    public User bangding(String openId, String phone, String password) {
        User user = userDao.findByUserName(phone);
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        if (!user.getUserPassword().equals(MD5.md5(password))) {
            throw new MyException(ResultEnum.USER_PASSWORDERROR);
        }
        user.setUserOpenId(openId);
        User save = userDao.save(user);
        save.setUserPassword("");
        return save;
    }
}
